#!/usr/bin/env bash
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common"
gem5_opt=
parsed=$(getopt -o "h${common_getopt_flags}" -l "$common_getopt_flags_long" -- "$@")
eval set -- "$parsed"
while true; do
  case "$1" in
    h)
      echo "$0 <exec-relative-path> [<brk-symbol>]" 2>&1
      exit
      ;;
    *)
      common_getopt_case "$@"
      ;;
  esac
done
executable_rel="$1"
shift
if [ "$#" -gt 0 ]; then
  brk="'$1'"
  shift
else
  brk=
fi
common_setup
if "$common_gem5"; then
  gem5_opt=-g
fi
executable="${common_build_dir}/${executable_rel}"
addr="$("${common_root_dir}/runtc" readelf -h "$executable" | awk '/Entry/{ print $NF }' )"
ex="-ex \"add-symbol-file $executable $addr\""
# -X or else lx-symbols throws for arm:
# gdb.MemoryError: Cannot access memory at address 0xbf0040cc
# TODO understand better.
#
# Also, lx-symbols overrides the add-symbol-file commands.
cmd="./rungdb -a '${common_arch}' -b '${ex}' ${gem5_opt} -X ${brk}"
echo "$cmd"
eval "$cmd"
